package com.ruoyi.logistic.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.logistic.domain.*;
import com.ruoyi.logistic.domain.vo.NetworkOutBoundSearchVo;
import com.ruoyi.logistic.mapper.NetworkOutboundSettlementMapper;
import com.ruoyi.logistic.mapper.OutboundQuotationMapper;
import com.ruoyi.logistic.mapper.QuotationMapper;
import com.ruoyi.logistic.mapper.QuotationTypeMapper;
import com.ruoyi.logistic.service.INetworkOutboundSettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class NetworkOutboundSettlementServiceImpl implements INetworkOutboundSettlementService {
    @Autowired
    private NetworkOutboundSettlementMapper networkOutboundSettlementMapper;
    @Autowired
    private QuotationMapper quotationMapper;
    @Autowired
    private QuotationTypeMapper quotationTypeMapper;
    @Autowired
    private OutboundQuotationMapper outboundQuotationMapper;
    @Override
    public List<NetworkOutboundSettlement> selectAll(PageDate pageDate) {
        PageHelper.startPage(pageDate);
        return networkOutboundSettlementMapper.selectAll();
    }

    @Override
    public Boolean insert(NetworkOutboundSettlement networkOutboundSettlement) {
        return networkOutboundSettlementMapper.insert(networkOutboundSettlement);
    }

    @Override
    public Boolean update(NetworkOutboundSettlement networkOutboundSettlement) {
//        if (networkOutboundSettlement.getQuotationId() != null){
//            Quotation quotation = quotationMapper.findById(networkOutboundSettlementMapper.findById(networkOutboundSettlement.getId()).getQuotationId());
//            networkOutboundSettlement.setQuotationType(quotation.getQuotationType().getTitle() + networkOutboundSettlement.getQuotation().getId());
//        }
        return networkOutboundSettlementMapper.update(networkOutboundSettlement);
    }

    @Override
    public Boolean deleteById(Integer id) {
        return networkOutboundSettlementMapper.deleteById(id);
    }

    @Override
    public NetworkOutboundSettlement findById(Integer id) {
        return networkOutboundSettlementMapper.findById(id);
    }

    @Override
    public List<NetworkOutboundSettlement> findByIds(Integer[] ids) {
        return networkOutboundSettlementMapper.findByIds(ids);
    }

    @Override
    public Integer deleteByIds(List<Integer> ids) {
        return networkOutboundSettlementMapper.deleteByIds(ids);
    }

    @Override
    public List<NetworkOutboundSettlement> search(NetworkOutBoundSearchVo networkOutboundSettlementVo) {
        PageHelper.startPage(networkOutboundSettlementVo.getPageDate());
        return networkOutboundSettlementMapper.search(networkOutboundSettlementVo);
    }


    @Override
    public Boolean insertMessage(OutboundQuotation outboundQuotation) {
        NetworkOutboundSettlement networkOutboundSettlement = networkOutboundSettlementMapper.findById(outboundQuotation.getOutboundId());
        Quotation quotation = quotationMapper.findById(outboundQuotation.getQuotationId());
        QuotationType quotationType = quotationTypeMapper.findById(quotation.getQuotationTypeId());

//        System.out.println("quotationType.getTitle() + quotationType.getId()" + quotationType.getTitle() + quotationType.getId());
        if (networkOutboundSettlement.getQuotationType() != null){
            networkOutboundSettlement.setQuotationType(networkOutboundSettlement.getQuotationType() + "," + quotationType.getTitle() + quotationType.getId());
        }else {
            networkOutboundSettlement.setQuotationType(quotationType.getTitle() + quotationType.getId());
        }
        networkOutboundSettlementMapper.update(networkOutboundSettlement);
        return outboundQuotationMapper.insert(outboundQuotation);
    }


}
